﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using WPFExcelReport.ViweModels;
using WPFExcelReport.Helpers;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    /// <summary>
    /// WorkFlowDocument display xps help documents
    /// </summary>
    public partial class WorkFlowDocument : DocumentBase
    {
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        bool IsSetUp = false;
        
        #region=========Property========= 
        public SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            } 
        }
 
        public SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            } 
        }
     
        public SETINTEXDB SetEnv
        {
            get
            {
                return vm.SetEnv;
            }
            set
            {
                vm.SetEnv = value;
            }
        }

        #region=====vwDetail=======
        public CollectionViewSource vwDetail
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwDetail");
            }
        }
        public DataRow dwDetail
        {
            get
            {
                if (vwDetail.View == null)
                    return null;
                if ((DataRowView)vwDetail.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwDetail.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwMaster=======
        public CollectionViewSource vwMaster
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMaster");
            }
        }
        public DataRow dwMaster
        {
            get
            {
                if (vwMaster.View == null)
                    return null;
                if ((DataRowView)vwMaster.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMaster.View.CurrentItem).Row;
            }
        }
        #endregion
        string KRS
        {
            get
            {
                return Helpers.Helper.Left(txtKRS.Text.ToString() + "                      ", 2);
            }
        }
        string LFD
        {
            get
            {
                return Helpers.Helper.Left("000000 " + txtLFD.Text.ToString().Trim(), 6);
            }
        }
        string WFCONTROL
        {
            get
            {
                if (ChkWFSTATUSJJ.IsChecked == true)
                {
                    return "JJ ";
                }
                if (ChkWFSTATUS.IsChecked==true)
                {
                    return "NO ";
                }
                else
                {
                    return "OK ";
                }
            }
        }
        private Dictionary<string, object> dicParamater
        {
            get
            {
                return vm.dicParamater;
            }
            set
            {
                vm.dicParamater = value;
            }
        }

        public string Mandant
        {
            get
            {
                return vm.Mandant;
            }
        }
        public string company
        {
            get
            {
                return vm.company;
            }
        }
        #endregion
        WorkFlowDocumentViewModel vm;
        /// <summary>
        /// Constructor
        /// </summary>
        public WorkFlowDocument()
        {
            InitializeComponent();
            this.Name = "工作流管理";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            { 
                base.SetUp(dicParamater);  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true; 
                vm = new WorkFlowDocumentViewModel();
                this.DataContext = vm;
                txtUserID.Text = vm.app.User;
                txtPassword.Password = vm.app.Pwd;
                vwMaster.View.CurrentChanged += vwMaster_CurrentChanged;
                ChkWFSTATUSJJ.IsChecked = false;
                ChkWFSTATUS.IsChecked = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMaster == null)
                return;
            SetDetail.Clear();
            try
            {
                DataRow dwNew = SetDetail.ICC_WFEVENTS.NewRow();
                dwNew.ItemArray = dwMaster.ItemArray;
                SetDetail.ICC_WFEVENTS.Rows.Add(dwNew);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnLogIn_Click(object sender, RoutedEventArgs e)
        {
            ZoneManager.Visibility = Visibility.Hidden;
            ZoneIT.Visibility = Visibility.Hidden;  
            string Mandant = txtMANDANT.Text.ToUpper();
            string userID = Helper.Left(txtUserID.Text.ToUpper() + "        ", 8);
            System.Security.SecureString SPassword = txtPassword.SecurePassword;
            IntPtr p = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(SPassword);
            string Password = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(p);
            DataRow dwHR = app.UserValid(userID, Password);
            if (dwHR == null)
            {
                MessageBox.Show("请检查登陆信息");
                return;
            }
            vm.UserID = userID;
            DataRow dwUser = app.GetUser(userID);
            if (dwUser == null)
                return;
            switch (dwUser["HRBGRP"].ToString())
            {
                case "EM":
                    ZoneManager.Visibility = Visibility.Visible;
                    ZoneIT.Visibility = Visibility.Hidden;
                    ZoneManager.IsEnabled = true;
                    break;
                case "YA":
                    ZoneManager.Visibility = Visibility.Visible;
                    ZoneIT.Visibility = Visibility.Visible;
                    ZoneIT.IsEnabled = true;
                    ZoneManager.IsEnabled = true;
                    break;
            }
            ZoneNormal.IsEnabled = true;
            expLog.IsExpanded = false;
            EventDispatcher.Instance.RaiseStatus(txtLogIn.Text, StatusEventType.UserLogIn);
        }
        private void New_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwMaster == null)
                    return;
                DataRow dwNew = SetDetail.ICC_WFEVENTS.NewRow();
                dwNew.ItemArray = dwMaster.ItemArray;
                dwNew["ROWNID"] = "0";
                dwNew["NEWUSER"] = vm.UserID;
                dwNew["NEWTIME"] = vm.date;
                SetDetail.ICC_WFEVENTS.Rows.Add(dwNew);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Load_Click(object sender, RoutedEventArgs e)
        {
            DataSet SResult = null;
            try
            {
                if (vm.UserID.Trim() == "")
                    return;
                DataRow dwData = SetEnv.ICC_WFEVENTS.NewRow();
                dwData["MANDANT"] = Mandant;
                dwData["WFEUSER"] = vm.UserID;
                dwData["WFSOURCE"] = vm.UserID;
                dwData["KRS"] = KRS;
                dwData["LFD"] = LFD;
                if (LFD == "000000")
                {
                    dwData["LFD"] = "";
                }            
                dwData["CONTROL"] =WFCONTROL;
                dwData["WFEVENTS"] =Helpers.Helper.Left( txtEvents.Text.ToString().PadRight(20),20);
                SResult = vm.Load(dwData);
                SetMaster.Clear();
                SetMaster.Merge(SResult);
                SetDetail.Merge(SetMaster);
                SetDetail.Clear();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetMaster.Clear();
                SetMaster.Merge(SResult);
            }
        }
        private void Copy_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                if (dwMaster == null)
                    return;
                DataRow dwNew = SetDetail.ICC_WFEVENTS.NewRow();
                dwNew.ItemArray = dwMaster.ItemArray;
                dwNew["ROWNID"] = "0";
                dwNew["NEWUSER"] = vm.UserID;
                dwNew["NEWTIME"] = vm.date;
                //NEWUSER,NEWTIME,CHGUSER,CHGTIME
                SetDetail.ICC_WFEVENTS.Rows.Add(dwNew);
                vwDetail.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                string WFEHANDLER = dwMaster["WFEHANDLER"].ToString();
                string WFEUSER = dwMaster["WFEUSER"].ToString();
                string WFEVENTS = dwMaster["WFEVENTS"].ToString();
                string ORDTYPE = dwMaster["ORDTYPE"].ToString();
                string KRS = dwMaster["KRS"].ToString();
                string LFD = dwMaster["LFD"].ToString();
                string ARTTYPE = dwMaster["ARTTYPE"].ToString();
                string ARTICLE = dwMaster["ARTICLE"].ToString();
                string COLOR = dwMaster["COLOR"].ToString();
                MessageInfo msg = new MessageInfo(MessageEventType.Info, WFEHANDLER, "WORKFLOW", dwMaster["ROWNID"].ToString());
                EventDispatcher.Instance.RaiseMessage(msg); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnWorkflowIDChange(string wfid)
        {
            base.OnWorkflowIDChange(wfid);
            try
            {
                dwWFEvent = null;
                DataSet SResult = vm.INTEXDBC.QueryICC_WFEVENTS(wfid, 1);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    dwWFEvent = row;
                }
                if (dwWFEvent == null)
                    return;
                string events = dwWFEvent["WFEVENTS"].ToString().Trim();
                switch (events)
                {
                    case "A001021"://主面料已经审核，辅料可以采购
               
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void CopyAll_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SetDetail.Clear();
                SetDetail.Merge(SetMaster.ICC_WFEVENTS);
                foreach (DataRow row in SetDetail.ICC_WFEVENTS.Rows)
                {
                    row["ROWNID"] = "0";
                    row["CHGUSER"] = vm.UserID;
                    row["CHGTIME"] = vm.date;
                    //NEWUSER,NEWTIME,CHGUSER,CHGTIME
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }

        }
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            DataSet setResult = null;
            try
            {
                vm.Save();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void butDiscard_Click(object sender, RoutedEventArgs e)
        {
            SetDetail.Clear();
            SetDetail.AcceptChanges();
        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {

            if (dwMaster == null)
            {
                MessageBox.Show("请先选择一条记录!");
                return;
            }
            vm.Delete(dwMaster);
            vwMaster.View.MoveCurrentTo(-1);
        }
        private void DeleteSameSource_Click(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("请先选择一条记录!");
                return;
            }
            vm.DeleteSameSource(dwMaster);
            SetMaster.AcceptChanges();
            vwMaster.View.MoveCurrentTo(-1);
        }

        private void EVENTS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            txtEvents.Text= (cb.SelectedValue).ToString();
        }

        private void USER_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            dwDetail["WFEUSER"] = cb.SelectedValue;
        }

        
    }
}
